package com.java.rabbitmq.topic;

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author Woo_home
 * @create 2020/5/27 13:33
 */

public class TestConsumerABC {

    // 队列名称
    public static final String EXCHANGE_NAME = "topics_exchange";

    public static void main(String[] args) throws Exception {

        // 创建连接
        Connection connection = ConnectionUtils.getConnection();

        // 创建通道
        Channel channel = connection.createChannel();

        // 声明交换机以及类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        // 获取一个临时队列
        String queueName = channel.queueDeclare().getQueue();

        // 接收 abc 消息
        channel.queueBind(queueName, EXCHANGE_NAME, "abc.*");

        System.out.println("[1]队列等待接收消息...");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("[1]队列接收到消息 " + message);
            }
        };
        // 自动应答机制
        channel.basicConsume(queueName, true, consumer);
    }
}
